package com.hzhr.iot.statis.service.helper;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class User_DeptServiceHelper {
    @Autowired
    User_DeptService user_deptService;

    public List<Integer> getDeptIdAndSubDeptIdListByDeptCode(Map<String, String> header, String deptCode) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "list");
        query.put("_is_paging", false);
        query.put("_like_code", deptCode + "%");
        List<DeptModel> deptModels = user_deptService.get(header, query).getModelList();

        List<Integer> deptIds = new ArrayList<>();
        for (DeptModel dept :
        deptModels) {
            deptIds.add(dept.getId());
        }

        return deptIds;
    }

    public List<Integer> getDeptIdListByResourceCode(Map<String, String> header, String resourceCode) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "list");
        query.put("_is_paging", false);
        query.put("_like_resourceCode", resourceCode + "%");
        List<DeptModel> deptModels = user_deptService.get(header, query).getModelList();

        List<Integer> deptIds = new ArrayList<>();
        for (DeptModel dept :
                deptModels) {
            deptIds.add(dept.getId());
        }

        return deptIds;
    }

    public DeptModel getById(Map<String, String> header, Integer id) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_id", id);
        DeptModel deptModel = user_deptService.get(header, query).getModel();

        return deptModel;
    }
}
